import xlTable from '../mixinUntil/xlTable'
import xlUntil from '../xlUntil/index'
import xlForm from '../mixinUntil/xlForm'
import stackForm from '../mixinUntil/stackForm'
import stackSee from '../mixinUntil/stackSee'
import seeForm from '../mixinUntil/seeForm'
import examineForm from '../mixinUntil/examineForm'
import service from '../../assets/service/require'

export default {
    name: "automation",
    data() {
        return {
            data: [],
            queryParam: {},
            paramMore:{},
            loading: true,
            pagination: {
                current: 1,
                pageSize: 10,
                pageSizeOptions: ['10', '20', '30'],
                showQuickJumper: true,
                showSizeChanger: true,
                total: 0,
                showTotal: total => `共 ${total} 条 `
            },
            selectedRowKeys: [],
            selectionRows: [],
            defaultImg: 'this.src="' + require('../../assets/images/defavatar.jpg') + '"'
        }
    },
    components: {
        xlTable,
        xlUntil,
        xlForm,
        stackForm,
        stackSee,
        seeForm,
        examineForm
    },
    created() {

    },
    methods: {
        pageOnload() {
            //加载数据
            this.loading = true
            return service({
                url: this.url.list,
                method: 'POST',
                data: {
                    pagingParam: {
                        pageSize: this.pagination.pageSize,
                        pageNumber: this.pagination.current
                    },
                    param: {
                        ...this.paramMore || {}, //引入页面的 额外参数
                        ...this.queryParam || {}
                    }
                }
                //过滤条件
            })
                .then(result => {
                    this.data = result.result.itemList
                    this.pagination.total = result.result.total || 0
                    this.loading = false
                    return result
                })
        },
        formatMerge() {
            let visibleLength = this.columns.filter(item => !item.tableVisible).length
            this.columns.map(item => {
                if (item.dataIndex) {
                    item.align = 'center'
                    item.ellipsis = true
                    // if (item.type == 'input') {
                    //     item.align = 'left'
                    // }
                    // if (item.type == 'input.number') {
                    //     item.align = 'right'
                    // }
                    // if (item.type == 'input.money') {
                    //     item.align = 'right'
                    // }
                    if (visibleLength > 9) {
                        if (!item.scopedSlots && item.key != 'rowIndex') {
                            item.scopedSlots = {customRender: 'tooltip'}
                            item.ellipsisF = true
                            if (!item.width) {
                                if (item.type == 'dateRange') {
                                    item.width = '250px'
                                } else {
                                    item.width = '200px'
                                }
                            }
                        }
                    } else {
                        if (!item.scopedSlots && item.key != 'rowIndex') {
                            item.ellipsisF = false
                            item.scopedSlots = {customRender: 'tooltip'}
                            if (!item.width) {
                                if(item.fixed){
                                    item.width = '229px'
                                }
                            }
                        }
                    }
                    if (item.key == 'rowIndex') {
                        item.ellipsisF = true
                        item.width = '80px'
                    }
                    if (item.dataIndex == 'action') {
                        item.ellipsisF = true
                        item.width = '150px'
                    }
                } else {
                    if (item.children) {
                        this.formatMergeChild(item.children)
                    }
                }
            })
        },
        formatMergeChild(children) {
            children.map(item => {
                if (item.dataIndex) {
                    item.ellipsis = true
                    item.align = 'center'
                    // if (item.type == 'input') {
                    //     item.align = 'left'
                    // }
                    // if (item.type == 'input.number') {
                    //     item.align = 'right'
                    // }
                    // if (item.type == 'input.money') {
                    //     item.align = 'right'
                    // }
                    if (!item.width) {
                        item.width = '175px'
                    }
                } else {
                    if (item.children) {
                        this.formatMergeChild(item.children)
                    }
                }
            })
        },
        addForm() {
            this.$refs.xlForm.tableTitle = '新增'
            this.$refs.xlForm.formatData(this.columns)
            this.$refs.xlForm.visible = true
            this.$refs.xlForm.confirmLoading = false
            this.$refs.xlForm.formatForm({})
            this.$refs.xlForm.type = 1
        },
        seeForm(data) {
            // 查看方法触发
            this.$refs.seeForm.formatData(this.columns, data)
            this.$refs.seeForm.visible = true
        },
        editForm(data) {
            //修改方法
            this.$refs.xlForm.tableTitle = '修改'
            this.$refs.xlForm.formatData(this.columns)
            this.$refs.xlForm.visible = true
            this.$refs.xlForm.confirmLoading = false
            this.$refs.xlForm.formatForm(data)
            this.$refs.xlForm.type = 2
            //赋值表单数据
        },
        examine(data) {
            this.$refs.examineForm.formatData(this.columns, data)
            this.$refs.examineForm.visible = true
            //赋值表单数据
        },
        searchKeyWord() {
            this.pagination.current = 1
            this.pageOnload()
        },
        delForm(record) {
            console.log(record)
            service({
                url: this.url.delete,
                method: 'POST',
                data: {
                    param: record
                }
            })
                .then(result => {
                    this.pageOnload()
                })
        },
        handleExamine(form){
            return service({
                url: this.url.examine,
                method: 'POST',
                data: {
                    param: {
                        ...this.paramMore || {},
                        ...form
                    }

                }
            })
                .then(result => {
                    this.$refs.examineForm.confirmLoading = false
                    this.$refs.examineForm.visible = false
                    this.pageOnload()
                })
                .catch(_ => {
                    this.$refs.examineForm.confirmLoading = false
                })
        },
        handleOk(form) {
            if (this.$refs.xlForm.type == 1) {
                return service({
                    url: this.url.add,
                    method: 'POST',
                    data: {
                        param: {
                            ...this.paramMore || {},
                            ...form

                        }

                    }
                })
                    .then(result => {
                        this.$refs.xlForm.confirmLoading = false
                        this.$refs.xlForm.visible = false
                        this.pageOnload()
                    })
                    .catch(_ => {
                        this.$refs.xlForm.confirmLoading = false
                    })
            } else {
                return service({
                    url: this.url.edit,
                    method: 'POST',
                    data: {
                        param: {
                            ...this.paramMore || {},
                            ...form
                        }
                    }
                })
                    .then(result => {
                        this.$refs.xlForm.confirmLoading = false
                        this.$refs.xlForm.visible = false
                        this.pageOnload()
                    })
                    .catch(_ => {
                        this.$refs.xlForm.confirmLoading = false
                    })
            }
        },
        handleTableChange(pagination, filters, sorter) {
            //页码方法
            this.loading = true
            this.pagination.current = pagination.current
            this.pagination.pageSize = pagination.pageSize
            return this.pageOnload()
            //翻页
        },
        onSelectChange(selectedRowKeys, selectionRows) {
            //多选框方法
            this.selectedRowKeys = selectedRowKeys;
            this.selectionRows = selectionRows;
        },
        delBatch() {
            this.$confirm({
                title: '提示',
                content: '确定删除' + (this.selectionRows.length) + '项数据吗？',
                onOk: () => {
                    console.log(this, 123)
                    let delData = this.selectionRows.map(item => {
                        return item.id
                    })
                    service({
                        url: this.url.deleteBatch,
                        method: 'POST',
                        data: {
                            param: delData

                        }
                    })
                        .then(result => {
                            this.pageOnload()
                        })
                },
                onCancel: () => {
                    console.log('Cancel');
                }
            });

        },
        qianFormat(num) {
            var reg = /\d{1,3}(?=(\d{3})+$)/g;
            return (num + '').replace(reg, '$&,');
        },
        // TODO 递归优化
        // array.map((item, index) => {
        //     let length = ['001', '002', '003']
        //     length.map((item2, index2) => {
        //         let index = array.findIndex(filter => filter == item2)
        //         if (index) {
        //             digui(array[index], index2)
        //         } else {
        //             result.push({
        //                 name: item2,
        //                 children: []
        //             })
        //         }
        //     })
        // })
        getCascaderData(data) {
            let extendData = {
                value: 'codeMasterValue',
                name: 'codeMasterName',
                codeSize: 3,
                dictionary: []
            }
            let assignData = Object.assign({}, extendData, data)
            if (typeof assignData.dictionary == 'string') {
                assignData.dictionary = this.$store.getters.dictionary[assignData.dictionary]
            }
            let result = []
            // 先构建级联菜单的第一层
            assignData.dictionary.filter(item => item[assignData.value].length === assignData.codeSize)
                .forEach(i => {
                    result.push({
                        value: i[assignData.value],
                        label: i[assignData.name],
                        children: []
                    })
                })
            if (result.length) {
                this.formUnscrambleTypeData(assignData.dictionary, result, assignData.codeSize, assignData)
            }
            return result
        },
        formUnscrambleTypeData(source, target, endIndex, assignData) {
            target.forEach(t => {
                source.filter(item => item[assignData.value].substring(0, endIndex) === t.value && item[assignData.value].length === t.value.length + assignData.codeSize)
                    .forEach(i => {
                        let child = {
                            value: i[assignData.value],
                            label: i[assignData.name],
                            children: []
                        }
                        t.children.push(child)
                    })
                if (t.children.length) {
                    this.formUnscrambleTypeData(source, t.children, endIndex + assignData.codeSize, assignData)
                } else {
                    // delete t.children
                }
            })
        }
    }
}
